﻿using UnityEngine;
using System.Collections;
using Dict.DataModel;
using System.Collections.Generic;

namespace Dict
{
    public class DictModuleBlo
    {
        static bool sorted = false;

        public static TableModule GetTableModule(string index)
        {
            return Dict.DictDataManager.Instance.tableModuleDao.GetById(index);
        }
        
        /// <summary>
        /// 获取等级附近三个解锁内容
        /// </summary>
        /// <param name="level"></param>
        /// <returns></returns>
        public static List<TableModule> GetThreeModulesNearLevel(int level)
        {
            List<TableModule> list = new List<TableModule>();
            int startIndex = Dict.DictDataManager.Instance.tableModuleDao.CacheList.Count;

            if (!sorted)
            {
                Dict.DictDataManager.Instance.tableModuleDao.CacheList.Sort(delegate (TableModule a, TableModule b)
                {
                    if (a.openLev < b.openLev)
                        return -1;
                    else if (a.openLev > b.openLev)
                        return 1;
                    return 0;
                });

                sorted = true;
            }
                
            for (int i = 0; i < Dict.DictDataManager.Instance.tableModuleDao.CacheList.Count; i++)
            {
                if (list.Count >= 3)
                    break;

                TableModule module = Dict.DictDataManager.Instance.tableModuleDao.CacheList[i];
                if(module.openLev >= level)
                {
                    if (i < startIndex)
                        startIndex = i;
                    list.Add(module);
                }
            }

            //向前补充
            if(list.Count < 3)
            {
                for (int i = startIndex - 1; i >= 0; i--)
                {
                    if (list.Count >= 3)
                        break;
                    list.Insert(0, Dict.DictDataManager.Instance.tableModuleDao.CacheList[i]);
                }
            }

            return list;
        }
    }
}
